<template>
  <div>
    <!-- <img src="http://localhost:9991/Upload/Tables/App_Appointment/201911240828293464/h52.jpg"> -->
    <div class="upload-container">
      <div class="upload-item">
        <div style="font-size: 13px; float: left; line-height: 32px">
          一级检查记录：
        </div>
        <VolUpload :url="url" :max-file="4" :excel="true" :multiple="true" :upload-before="uploadBefore"
          :upload-after="uploadAfter" :yjfileInfo="yjfileInfo" :on-change="onChange" style="float: left; width: 55%">
        </VolUpload>

        <div style="font-size: 13px; color: red; clear: both">
          说明：文件格式要求为：pdf/zip/rar,优先使用pdf格式
        </div>
        <div style="font-size: 13px; color: red; clear: both">
          <p>
            注意：一级检查记录应包括：&nbsp;&nbsp;1.DOM套合差统计表和附件资料&nbsp;2.监测要素（国家规定格式）&nbsp;3.元数据检查记录表&nbsp;4.实地照片与外业调查文件&nbsp;
          </p>
        </div>
      </div>
      <div class="upload-item">
        <div style="font-size: 13px; float: left; line-height: 32px">
          二级检查记录：
        </div>
        <ljVolUpload :multiple="true" :max-file="4" :excel="true" :ljfileInfo="ljfileInfo" :url="url"  
          :upload-before="ljuploadBefore" :upload-after="ljuploadAfter" :on-change="ljonChange"
          style="float: left; width: 55%">
        </ljVolUpload>

        <div style="font-size: 13px; color: red; clear: both">
          说明：文件格式要求为：pdf/zip/rar,优先使用pdf格式
        </div>
        <div style="font-size: 13px; color: red; clear: both">
          <p>
            注意：二级检查记录应包括：&nbsp;&nbsp;1.附件资料和一查检查记录表&nbsp;2.监测要素&nbsp;3.元数据检查记录表&nbsp;4.实地照片与外业调查文件
          </p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import ljVolUpload from "@/components/basic/ljjlVolUpload.vue";
import VolUpload from "@/components/basic/yjjlVolUpload.vue";
export default {
  components: { VolUpload, ljVolUpload },
  data() {
    return {
      url: "/api/zm_data_record/uploads",
      model: true,
      yjfileInfo: [],
      ljfileInfo: [],
      loadingStatus: false,
    };
  },
  created() { },
  mounted() {
    console.log("12", this.$store.state.yjchecked);
  },
  methods: {
    removeFile(index) {
      this.yjfileInfo.splice(index, 1);
    },
    uploadBefore1(files) {
      debugger;
      console.log("123")
      this.yjfileInfo.splice(0);
      for (let index = 0; index < files.length; index++) {
        const element = files[index];
        this.yjfileInfo.push({ name: element.name });
      }
      console.log("yjfileInfo", this.yjfileInfo);
      return true;
    },
    ljremoveFile(index) {
      this.ljfileInfo.splice(index, 1);
    },
    ljuploadBefore1(files) {
      this.ljfileInfo.splice(0);
      for (let index = 0; index < files.length; index++) {
        const element = files[index];
        this.jjbgfljfileInfoileInfo.push({ name: element.name });
      }
      return true;
    },
    ljuploadAfter1(result, files) {
      if (!result.status) return true;
      // this.fileInfo.forEach(x => {
      //   x.path = result.data;
      // });
      return true;
    },
    fileClick(index, file) {
      if (
        !file.path &&
        file.name.indexOf("/") == -1 &&
        file.name.indexOf("\\") == -1
      ) {
        return this.$Message.error({ duration: 5, content: "请先上传此文件" });
      }
      //从api服务器下载
      if (!this.base.checkUrl(file.path)) {
        this.dowloadFile(
          this.http.ipAddress +
          "api/App_Appointment/DownLoadFile?path=" +
          file.path +
          file.name,
          file.name,
          { "": this.http }
        );
        return;
      }
      //从远程下载
      this.$refs.downFile.href = file.path || file.name;
      this.$refs.downFile.setAttribute("download", "download");
      this.$refs.downFile.click();
    },
    getFileNames(files) {
      let arr = [];
      for (let index = 0; index < files.length; index++) {
        const element = files[index];
        arr.push(element.name);
      }
      return arr.join(",");
    },
    uploadBefore(files) {
      var rest = true;
      var filename = this.getFileNames(files);
      var filenameArrt = filename.split(",");
      console.log(filenameArrt, "")
      var filterMsg = [];
      var filetype = [".pdf", ".zip", ".rar"];
      if (filenameArrt.length == 4) {
        for (var i = 0; i < filenameArrt.length; i++) {
          var file = filenameArrt[i];
          var filter = file.substring(file.lastIndexOf(".")).toLowerCase();
          if (!filetype.includes(filter)) {
            filterMsg.push(file);
          }
        }

        if (filenameArrt[0].includes("DOM套合差") == false) {
          if (filenameArrt[1].includes("DOM套合差") == false) {
            if (filenameArrt[2].includes("DOM套合差") == false) {
              if (filenameArrt[3].includes("DOM套合差") == false) {
                this.$alert("请选择相关DOM套合差文件！", "消息提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.yjfileInfo = [];
                  },
                });
                return;
              }
            }
          }
        }
        if (filenameArrt[0].includes("监测要素") == false) {
          if (filenameArrt[1].includes("监测要素") == false) {
            if (filenameArrt[2].includes("监测要素") == false) {
              if (filenameArrt[3].includes("监测要素") == false) {
                this.$alert("请选择相关监测要素文件！", "消息提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.yjfileInfo = [];
                  },
                });
                return;
              }
            }
          }
        }
        if (filenameArrt[0].includes("元数据检查记录表") == false) {
          if (filenameArrt[1].includes("元数据检查记录表") == false) {
            if (filenameArrt[2].includes("元数据检查记录表") == false) {
              if (filenameArrt[3].includes("元数据检查记录表") == false) {
                this.$alert("请选择相关元数据检查记录表文件！", "消息提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.yjfileInfo = [];
                  },
                });
                return;
              }
            }
          }
        }
        if (filenameArrt[0].includes("实地照片与外业调查文件") == false) {
          if (filenameArrt[1].includes("实地照片与外业调查文件") == false) {
            if (filenameArrt[2].includes("实地照片与外业调查文件") == false) {
              if (filenameArrt[3].includes("实地照片与外业调查文件") == false) {
                this.$alert("请选择相关实地照片与外业调查文件！", "消息提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.yjfileInfo = [];
                  },
                });
                return;
              }
            }
          }
        }
        if (filenameArrt[0].includes("实地照片与外业调查文件") == false && filenameArrt[0].includes("元数据检查记录表") == false && filenameArrt[0].includes("DOM套合差") == false && filenameArrt[0].includes("监测要素") == false) {
          this.$alert("文件" + filenameArrt[0] + "中需包含DOM套合差、监测要素、元数据检查记录表、实地照片与外业调查文件等字样", "消息提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.yjfileInfo = [];
            },
          });
          return;
        }
        if (filenameArrt[1].includes("实地照片与外业调查文件") == false && filenameArrt[1].includes("元数据检查记录表") == false && filenameArrt[1].includes("DOM套合差") == false && filenameArrt[1].includes("监测要素") == false) {
          this.$alert("文件" + filenameArrt[1] + "中需包含DOM套合差、监测要素、元数据检查记录表、实地照片与外业调查文件等字样", "消息提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.yjfileInfo = [];
            },
          });
          return;
        }
        if (filenameArrt[2].includes("实地照片与外业调查文件") == false && filenameArrt[2].includes("元数据检查记录表") == false && filenameArrt[2].includes("DOM套合差") == false && filenameArrt[2].includes("监测要素") == false) {
          this.$alert("文件" + filenameArrt[2] + "中需包含DOM套合差、监测要素、元数据检查记录表、实地照片与外业调查文件等字样", "消息提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.yjfileInfo = [];
            },
          });
          return;
        }
        if (filenameArrt[3].includes("实地照片与外业调查文件") == false && filenameArrt[3].includes("元数据检查记录表") == false && filenameArrt[3].includes("DOM套合差") == false && filenameArrt[3].includes("监测要素") == false) {
          this.$alert("文件" + filenameArrt[3] + "中需包含DOM套合差、监测要素、元数据检查记录表、实地照片与外业调查文件等字样", "消息提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.yjfileInfo = [];
            },
          });
          return;
        }
        if (filterMsg.length > 0) {
          console.log("filterMsg", filterMsg);
          this.$alert("上传文件格式不正确，请重新上传！", "消息提示", {
            confirmButtonText: "确定",
            type: "error",
            callback: (action) => {
              this.yjfileInfo = [];
            },
          });
          return;
        }
      } else {
        this.$alert("上传文件数量不正确，请核实后重新上传！", "消息提示", {
          confirmButtonText: "确定",
          type: "error",
          callback: (action) => {
            this.yjfileInfo = [];
          },
        });
        return;
      }
      return rest;
    },
    uploadAfter(result, files) {
      console.log("后触发yj");
      var rest = true;
      var filename = this.getFileNames(files);
      var filenameArrt = filename.split(",");
      var filterMsg = [];
      var sizeErrorMsg = []; // For files exceeding size limit
      var filetype = [".pdf", ".zip", ".rar"];
       const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB limit
      if (filenameArrt.length == 4) {
        for (var i = 0; i < filenameArrt.length; i++) {
          var file = filenameArrt[i];
          var filter = file.substring(file.lastIndexOf(".")).toLowerCase();
          if (!filetype.includes(filter)) {
            filterMsg.push(file);
          }
          if (files[i].size > MAX_FILE_SIZE) {
            sizeErrorMsg.push(file);
            rest = false;
         }
        }

        if (filterMsg.length > 0) {
          console.log("filterMsg", filterMsg);
          this.$alert("上传文件格式不正确，请重新上传！", "消息提示", {
            confirmButtonText: "确定",
            type: "error",
            callback: (action) => {
              this.yjfileInfo = [];
            },
          });
        }
         if (sizeErrorMsg.length > 0) {
        console.log("sizeErrorMsg", sizeErrorMsg);
        this.$alert("以下文件大小超过10MB限制: " + sizeErrorMsg.join(", "), "消息提示", {
            confirmButtonText: "确定",
            type: "error",
            callback: (action) => {
                this.yjfileInfo = [];
            },
        });
        rest = false;
    }

        
      } else {
        this.$alert("上传文件数量不正确，请核实后重新上传！", "消息提示", {
          confirmButtonText: "确定",
          type: "error",
          callback: (action) => {
            this.yjfileInfo = [];
          },
        });
        return;
      }
      return rest;
    },
    onChange(files) {
      // console.log("选择的文件:" + +this.getFileNames(files));
      return true;
    },

    ljuploadBefore(files) {
      var rest = true;
      var filename = this.getFileNames(files);
      var filenameArrt = filename.split(",");
      var filterMsg = [];
      var filetype = [".pdf", ".zip", ".rar"];
      for (var i = 0; i < filenameArrt.length; i++) {
        var file = filenameArrt[i];
        var filter = file.substring(file.lastIndexOf(".")).toLowerCase();
        if (!filetype.includes(filter)) {
          filterMsg.push(file);
        }
      }
 if (filenameArrt[0].includes("附件资料和一查检查记录表") == false) {
          if (filenameArrt[1].includes("附件资料和一查检查记录表") == false) {
            if (filenameArrt[2].includes("附件资料和一查检查记录表") == false) {
              if (filenameArrt[3].includes("附件资料和一查检查记录表") == false) {
                this.$alert("请选择相关附件资料和一查检查记录表文件！", "消息提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.ljfileInfo = [];
                  },
                });
                return;
              }
            }
          }
        }
        if (filenameArrt[0].includes("监测要素") == false) {
          if (filenameArrt[1].includes("监测要素") == false) {
            if (filenameArrt[2].includes("监测要素") == false) {
              if (filenameArrt[3].includes("监测要素") == false) {
                this.$alert("请选择相关监测要素文件！", "消息提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.ljfileInfo = [];
                  },
                });
                return;
              }
            }
          }
        }
        if (filenameArrt[0].includes("元数据检查记录表") == false) {
          if (filenameArrt[1].includes("元数据检查记录表") == false) {
            if (filenameArrt[2].includes("元数据检查记录表") == false) {
              if (filenameArrt[3].includes("元数据检查记录表") == false) {
                this.$alert("请选择相关元数据检查记录表文件！", "消息提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.ljfileInfo = [];
                  },
                });
                return;
              }
            }
          }
        }
        if (filenameArrt[0].includes("实地照片与外业调查文件") == false) {
          if (filenameArrt[1].includes("实地照片与外业调查文件") == false) {
            if (filenameArrt[2].includes("实地照片与外业调查文件") == false) {
              if (filenameArrt[3].includes("实地照片与外业调查文件") == false) {
                this.$alert("请选择相关实地照片与外业调查文件！", "消息提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.ljfileInfo = [];
                  },
                });
                return;
              }
            }
          }
        }
        if (filenameArrt[0].includes("实地照片与外业调查文件") == false && filenameArrt[0].includes("元数据检查记录表") == false && filenameArrt[0].includes("附件资料和一查检查记录表") == false && filenameArrt[0].includes("监测要素") == false) {
          this.$alert("文件" + filenameArrt[0] + "中需包含附件资料和一查检查记录表、监测要素、元数据检查记录表、实地照片与外业调查文件等字样", "消息提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.ljfileInfo = [];
            },
          });
          return;
        }
        if (filenameArrt[1].includes("实地照片与外业调查文件") == false && filenameArrt[1].includes("元数据检查记录表") == false && filenameArrt[1].includes("附件资料和一查检查记录表") == false && filenameArrt[1].includes("监测要素") == false) {
          this.$alert("文件" + filenameArrt[1] + "中需包含附件资料和一查检查记录表、监测要素、元数据检查记录表、实地照片与外业调查文件等字样", "消息提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.ljfileInfo = [];
            },
          });
          return;
        }
        if (filenameArrt[2].includes("实地照片与外业调查文件") == false && filenameArrt[2].includes("元数据检查记录表") == false && filenameArrt[2].includes("附件资料和一查检查记录表") == false && filenameArrt[2].includes("监测要素") == false) {
          this.$alert("文件" + filenameArrt[2] + "中需包含附件资料和一查检查记录表、监测要素、元数据检查记录表、实地照片与外业调查文件等字样", "消息提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.ljfileInfo = [];
            },
          });
          return;
        }
        if (filenameArrt[3].includes("实地照片与外业调查文件") == false && filenameArrt[3].includes("元数据检查记录表") == false && filenameArrt[3].includes("附件资料和一查检查记录表") == false && filenameArrt[3].includes("监测要素") == false) {
          this.$alert("文件" + filenameArrt[3] + "中需包含附件资料和一查检查记录表、监测要素、元数据检查记录表、实地照片与外业调查文件等字样", "消息提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.ljfileInfo = [];
            },
          });
          return;
        }


      
      if (filterMsg.length > 0) {
        console.log("filterMsg", filterMsg);
        this.$alert("上传文件格式不正确，请重新上传！", "消息提示", {
          confirmButtonText: "确定",
          type: "error",
          callback: (action) => {
            this.ljfileInfo = [];
          },
        });
        return;
      }
      return rest;
    },
    ljuploadAfter(result, files) {
      var rest = true;
      console.log("两检")
      var rest = true;

      var filename = this.getFileNames(files);
      var filenameArrt = filename.split(",");
      var filterMsg = [];
      var sizeErrorMsg = []; // For files exceeding size limit
      var filetype = [".pdf", ".zip", ".rar"];
       const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB limit
      var filetype = [".pdf", ".zip", ".rar"];
      if (filenameArrt.length == 4) {
        for (var i = 0; i < filenameArrt.length; i++) {
          var file = filenameArrt[i];
          var filter = file.substring(file.lastIndexOf(".")).toLowerCase();
          if (!filetype.includes(filter)) {
            filterMsg.push(file);
          }
          if (files[i].size > MAX_FILE_SIZE) {
            sizeErrorMsg.push(file);
            rest = false;
         }
        }

        if (filterMsg.length > 0) {
          console.log("filterMsg", filterMsg);
          this.$alert("上传文件格式不正确，请重新上传！", "消息提示", {
            confirmButtonText: "确定",
            type: "error",
            callback: (action) => {
              this.ljfileInfo = [];
            },
          });
          return;
        }
         if (sizeErrorMsg.length > 0) {
        console.log("sizeErrorMsg", sizeErrorMsg);
        this.$alert("以下文件大小超过10MB限制: " + sizeErrorMsg.join(", "), "消息提示", {
            confirmButtonText: "确定",
            type: "error",
            callback: (action) => {
                this.ljfileInfo = [];
            },
        });
        rest = false;
    }
      } else {
        this.$alert("上传文件数量不正确，请核实后重新上传！", "消息提示", {
          confirmButtonText: "确定",
          type: "error",
          callback: (action) => {
            this.ljfileInfo = [];
          },
        });
        return;
      }

      return rest;
    },
    ljonChange(files) {
      // console.log("选择的文件:" + +this.getFileNames(files));
      return true;
    },
    dowloadFile(url, fileName) {
      this.base.dowloadFile(url, fileName, {
        Authorization: this.$store.getters.getToken(),
      });
    },
  },
};
</script>
<style lang="less" scoped>
.upload-container {
  max-height: 240px;
  display: flex;

  .upload-item {
    flex: 1;
    padding: 20px;
  }
}
</style>
